package comp.wxapi.bean;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

public class Result<T> implements Serializable {
	
	private static final long serialVersionUID = 2284796388441622817L;

	private String msg;
	
	private T data;
	
	private boolean success;

	public static <T> Result<?> ok() {
		
		return new Result<T>("成功", true);
	}
	
	public static <T> Result<T> ok(String msg,T t) {
		
		return new Result<T>(msg,t,true);
	}
	
	public static <T> Result<T> ok(T data) {
		
		return new Result<T>("成功",data,true);
	}
	
	public static <T> Result<T> error(String msg) {
		
		return new Result<T>(msg,false);
	}
	
	public Result() {}
	
	public Result(String msg,boolean success) {
		this(msg, null, success);
	}
	
	public Result(String msg, T data, boolean success) {
		super();
		this.msg = msg;
		this.data = data;
		this.success = success;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public Result<T> addData(String key,Object value) {
		Map<String,Object> map = null;
		if(this.data == null) {
			map = new HashMap<String,Object>();
		}else {
			map = (Map)this.data;
		}
		map.put(key, value);
		this.data = (T)map;
		return this;
	}
	
	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}
	
	public static void main(String[] args) {
		
	}
}
